using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using ShopForWeb.Services;
using ShopForWeb.UI.Models;
using ShopForWeb.UI.Utils.Extensions;

namespace ShopForWeb.UI.Areas.Admin
{
    [Area("admin")]
    [Route("[area]/[controller]")]
    public class IdentityController : Controller
    {
        private readonly ILogger<IdentityController> _logger;
        private AdminService _adminService;

        public IdentityController(ILogger<IdentityController> logger,AdminService adminService)
        {
            _logger = logger;
            _adminService=adminService;
        }

        public IActionResult Index()
        {
            return View();
        }
        [HttpPost("login")]
    public async Task<IActionResult> LoginAsync([FromForm] string phone, [FromForm] string password)
    {
      var admin = await _adminService.GetByLoginAsync(phone, password);
      if (admin == null)
      {
        return Json(new ResponseViewModel()
        {
          Code = 500,
          Message = "查无此人"
        });
      }
      else
      {
        this.SetAdmin(admin);//存根
        return Json(new ResponseViewModel()
        {
          Code = 200,
          Message = $"欢迎{admin.AdminName}的到来"
        });
      }
    }
    [Route("logout")]
    public IActionResult Logout()
    {
      var session = this.HttpContext.Session;
      session.Clear();
      return Json(new ResponseViewModel()
      {
        Code = 200,
        Message = "安全登出"
      });
    }
    }
}